<template>
  <page-header-wrapper :title="false">
    <a-card class="content-card">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="评论内容">
                <a-input v-model="queryParam.content" placeholder="评论内容" allowClear />
              </a-form-item>
            </a-col>
            <a-col :md="!advanced && 6 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="getSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table 
        size="middle"
        :columns="columns" 
        :data-source="data" 
        :rowKey="r=>r._id"
        :pagination="pagination"
        :scroll="{x: true}"
        @change="paginationChange"
        :loading="loading"
      >
        <template slot="product" slot-scope="r">
          {{ r.productId.title }}
        </template>
        <template slot="area" slot-scope="t,r">
          {{ r.province }} {{ r.city }} {{ r.region }}
        </template>
        <span slot="action" slot-scope="r">
          <a-popconfirm
            title="您确定要删除吗?"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteClick(r)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { toggleQuery,showMessage } from '@/utils/mixins'
import { getMessageList,deleteMessage,replyMessage } from '@/api/manage'

export default {
  name: 'Message',
  mixins: [toggleQuery,showMessage],
  data () {
    return {
      queryParam: {},
      columns: [
        {title: '序号',customRender: (t,r,i) => `${i+1}`,width: 70,align: 'center'},
        {title: '回复时间',dataIndex: 'createdAt',key: 'createdAt',width: 180},
        {title: '咨询产品',scopedSlots: {customRender: 'product'},width: 240},
        {title: '客户姓名',dataIndex: 'customerName',key: 'customerName',width: 120},
        {title: '联系方式',dataIndex: 'phoneNumber',key: 'phoneNumber',width: 150},
        {title: '购买周期', dataIndex: 'region',key: 'region',width: 120},
        {title: '公司名称',dataIndex: 'companyName',key: 'companyName',width: 260},
        {title: '所在地区',scopedSlots: {customRender: 'area'},width: 200},
        {title: '回复内容',dataIndex: 'askFor',key: 'askFor'},
        {title: '操作',scopedSlots: {customRender: 'action'},width: 140,align: 'center',fixed: 'right'}
      ],
      data: [],
      loading: false,
      pagination: {
        total: 0,
        pageSize: 10,
        showTotal: total => `共 ${total} 条`
        // showSizeChanger: true,
        // pageSizeOptions: ['10', '20', '30', '40']
      },
      pageNum: 1,
      pageSize: 10
    }
  },
  computed: {
    _width() {
      return this.columns.reduce((x,y) => {x+ y.width},0)
    }
  },
  methods: {
    // 分页查询
    paginationChange(e) {
      this.pageNum = e.current
      this.pageSize = e.pageSize
      this.getList()
    },
    // 查询
    getSearch() {
      this.pageNum = 1
      this.getList()
    },
    // 删除
    handleDeleteClick(r) {
      deleteMessage({id: r._id}).then(res => {
        if(res.code == 200) {
          this.showMessage(res,this.getList)
        }
      })
    },
    // 获取列表
    getList() {
      this.loading = true
      getMessageList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.queryParam
      }).then(res => {
        this.loading = false
        if(res.code == 200) {
          console.log(res.data)
          this.data = res.data.list
          this.pagination.total = res.data.total
        }
      })
    }
  },
  mounted() {
    this.getList()
    console.log(this._width)
  }
}
</script>